package cn.yr.clock.handler;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 微信小程序处理器
 */
@Component
public class AppletHandler {

    /**
     * 获取openId
     */
    private String authLoginUrl = "https://api.weixin.qq.com/sns/jscode2session";

    @Value("${applet.appId}")
    private String appId;

    @Value("${applet.secret}")
    private String secret;


    /**
     * 获取openId
     * @param code
     * @return
     */
    public String getOpenId(String code){
        HashMap<String, Object> map = new HashMap<>(4);
        map.put("appid", appId);
        map.put("secret", secret);
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");
        String res = HttpUtil.get(" https://api.weixin.qq.com/sns/jscode2session", map);
        return JSONUtil.parseObj(res).getStr("openid");
    }

}
